<template>
  <div class="login-container">
    <div v-if="$global.lang" class="app-lang">
      <lang color="#409eff"></lang>
    </div>
    <div class="login-form">
      <div class="form-user" @keypress="loginPress">
        <div class="login-text">
          <div>
            <div>光谷医院({{ $ts("账号登录") }})...</div>
            <div class="login-line"></div>
          </div>
          <div style="flex: 1"></div>
        </div>
        <div class="login-text-small">WELCOME TO LOGIN</div>
        <div class="item">
          <div class="input-icon el-icon-user"></div>
          <input
            type="text"
            v-focus
            v-model="userInfo.userName"
            :placeholder="$ts(['请输入', '账号'])"
          />
        </div>
        <div class="item">
          <div class="input-icon el-icon-lock"></div>
          <input
            type="password"
            v-focus
            v-model="userInfo.password"
            :placeholder="$ts(['请输入', '密码'])"
          />
        </div>
        <div class="item">
          <div class="input-icon el-icon-mobile"></div>

          <input
            v-focus
            type="text"
            v-model="userInfo.verificationCode"
            :placeholder="$ts(['请输入', '验证码'])"
          />
          <div class="code" @click="getVierificationCode">
            <img v-show="codeImgSrc != ''" :src="codeImgSrc" />
          </div>
        </div>
      </div>
      <div class="loging-btn">
        <el-button
          size="large"
          :loading="loading"
          color="#3a6cd1"
          :dark="true"
          @click="login"
          long
        >
          <span v-if="!loading">{{ $ts("登录") }}</span>
          <span v-else>{{ $ts("正在登录") }}...</span>
        </el-button>
      </div>
    </div>
  </div>
</template>

<script>
import { defineComponent, ref, reactive, toRefs, getCurrentInstance } from "vue";
import { useRouter, useRoute } from "vue-router";
import store from "../store/index";
import http from "@/../src/api/http.js";
import lang from "@/components/lang/lang";
export default defineComponent({
  components: {
    lang,
  },
  setup(props, context) {
    const loading = ref(false);
    const codeImgSrc = ref("");
    const userInfo = reactive({
      userName: "",
      password: "",
      verificationCode: "",
      UUID: undefined,
    });

    const getVierificationCode = () => {
      http.get("/api/User/getVierificationCode").then((x) => {
        codeImgSrc.value = "data:image/png;base64," + x.img;
        userInfo.UUID = x.uuid;
      });
    };
    getVierificationCode();
    let router = useRouter();
    const { proxy } = getCurrentInstance();
    const { $message, $ts } = proxy;
    const login = () => {
      if (!userInfo.userName) return $message.error($ts(["请输入", "账号"]));
      if (!userInfo.password) return $message.error($ts(["请输入", "密码"]));
      if (!userInfo.verificationCode) {
        return $message.error($ts(["请输入", "验证码"]));
      }
      loading.value = true;
      http.post("/api/user/login", userInfo, $ts("正在登录") + "....").then((result) => {
        if (!result.status) {
          loading.value = false;
          getVierificationCode();
          return $message.error(result.message);
        }
        //  $message.success($ts("登录成功,正在跳转!"));
        store.commit("setUserInfo", result.data);
        router.push({ path: "/" });
      });
    };
    const loginPress = (e) => {
      if (e.keyCode == 13) {
        login();
      }
    };
    const openUrl = (url) => {
      window.open(url, "_blank");
    };
    return {
      loading,
      codeImgSrc,
      getVierificationCode,
      login,
      userInfo,
      loginPress,
      openUrl,
    };
  },
  directives: {
    focus: {
      inserted: function (el) {
        el.focus();
      },
    },
  },
});
</script>
<style lang="less" scoped>
.login-container {
  display: flex;
  width: 100%;
  height: 100%;
  background: url('https://pic.vjshi.com/2022-10-11/04492106556b4c3db370d55ed45e83f3/00003.jpg?x-oss-process=style/watermark') center center no-repeat;
  background-size: cover;
  background-position: center;
  justify-content: center;
  align-items: flex-end;
  position: relative;
  padding-bottom: 15%;
  
  &::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: linear-gradient(
      to bottom,
      rgba(0, 0, 0, 0.1),
      rgba(0, 0, 0, 0.5)
    );
    backdrop-filter: blur(0px);
  }
}

.login-form {
  position: relative;
  align-items: center;
  width: 380px;
  display: flex;
  flex-direction: column;
  background: rgba(0, 0, 0, 0.6);
  padding: 40px 35px;
  box-shadow: 0 15px 35px rgba(0, 0, 0, 0.4);
  border-radius: 16px;
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  border: 1px solid rgba(255, 255, 255, 0.1);
}

.form-user .item {
  border-radius: 12px;
  border: 1px solid rgba(255, 255, 255, 0.2);
  display: flex;
  margin-bottom: 25px;
  background: rgba(255, 255, 255, 0.1);
  height: 50px;
  padding-left: 20px;
  transition: all 0.3s ease;

  &:hover, &:focus-within {
    border-color: rgba(255, 255, 255, 0.4);
    background: rgba(255, 255, 255, 0.15);
  }

  .code {
    position: relative;
    cursor: pointer;
    width: 100px;
    padding: 8px 15px 0 0;
    
    img {
      border-radius: 6px;
      height: 36px;
    }
  }

  .input-icon {
    line-height: 50px;
    color: rgba(255, 255, 255, 0.8);
    padding-right: 15px;
    font-size: 18px;
  }
}

input {
  background: transparent !important;
  display: block;
  box-sizing: border-box;
  width: 100%;
  min-width: 0;
  margin: 0;
  padding: 0;
  color: #ffffff !important;
  line-height: inherit;
  text-align: left;
  border: 0;
  outline: none;
  font-size: 16px;
  line-height: 50px;

  &::placeholder {
    color: rgba(255, 255, 255, 0.5);
  }
}

.login-text {
  font-weight: 600;
  font-size: 26px;
  letter-spacing: 1px;
  position: relative;
  display: flex;
  color: #ffffff;
  margin-bottom: 10px;
  
  .login-line {
    display: none;
  }
}

.login-text-small {
  margin-bottom: 35px;
  font-size: 15px;
  color: rgba(255, 255, 255, 0.7);
  letter-spacing: 1px;
}

.loging-btn {
  width: 100%;
  margin-top: 10px;
  
  button {
    background: #409eff !important;
    height: 50px;
    font-size: 16px !important;
    width: 100%;
    border: none;
    border-radius: 12px;
    font-weight: 600;
    letter-spacing: 1px;
    transition: all 0.3s ease;
    
    &:hover {
      transform: translateY(-2px);
      box-shadow: 0 5px 15px rgba(64, 158, 255, 0.3);
    }
  }
}

// 移除不需要的样式
.app-link, .account-info, .login-footer {
  display: none;
}

// 移动端适配
@media screen and (max-width: 700px) {
  .login-container {
    padding: 20px;
    padding-bottom: 10%;
    align-items: flex-end;
  }

  .login-form {
    width: 100%;
    padding: 30px 20px;
  }

  .app-link {
    flex-wrap: wrap;
    
    a {
      margin: 10px;
    }
  }
}
</style>
<style lang="less" scoped>
.app-lang {
  position: absolute;
  z-index: 999;
  right: 60px;
  top: 20px;
}
</style>
